package cn.inno.admin;

import cn.inno.dao.SysAdminMapper;
import cn.inno.entitys.ErrorMsg;
import cn.inno.entitys.SysAdmin;
import cn.inno.entitys.SysAdminExample;
import cn.inno.exceptions.BusinessException;
import cn.inno.service.BaseService;
import cn.inno.util.IdUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 管理员服务类
 *
 * @author LeungZengJian
 */
@Service
public class AdminService extends BaseService {

    @Autowired
    private AdminDao adminDao;
    @Autowired
    private SysAdminMapper sysAdminMapper;

    public JSONObject adminList(String adminId) {

        Map formMap = new HashMap();
        formMap.put("adminId", adminId);
        List lists = this.adminDao.adminList(formMap);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put(KEY_DATA, lists);

        return executeRerutn(jsonObject);
    }

    @Transactional
    public JSONObject adminAdd(SysAdmin sysAdmin, String adminId) {

        this.check(sysAdmin);

        sysAdmin.setAdminId(IdUtil.getInstance().getPkId());
        sysAdmin.setSupAdmin(adminId);
        sysAdmin.setAddName(adminId);
        sysAdmin.setAddTime(new Date());

        this.sysAdminMapper.insertSelective(sysAdmin);

        JSONObject jsonObject = new JSONObject();
        return executeRerutn(jsonObject);
    }

    @Transactional
    public JSONObject adminDel(String adminIds, String adminId) {

        List<String> result = Arrays.asList(adminIds.split(","));

        Map formMap = new HashMap();

        formMap.put("adminIds", result);

        this.adminDao.adminDel(formMap);

        JSONObject jsonObject = new JSONObject();
        return executeRerutn(jsonObject);
    }

    @Transactional
    public JSONObject adminUp(SysAdmin sysAdmin, String adminId) {

        this.check(sysAdmin);

        sysAdmin.setUpName(adminId);
        sysAdmin.setUpTime(new Date());

        this.sysAdminMapper.updateByPrimaryKeySelective(sysAdmin);

        JSONObject jsonObject = new JSONObject();
        return executeRerutn(jsonObject);
    }

    @Transactional
    public JSONObject adminUpStatus(SysAdmin sysAdmin, String adminId) {

        sysAdmin.setUpName(adminId);
        sysAdmin.setUpTime(new Date());

        this.sysAdminMapper.updateByPrimaryKeySelective(sysAdmin);

        JSONObject jsonObject = new JSONObject();
        return executeRerutn(jsonObject);
    }

    public JSONObject adminEdit(String adminId) {

        SysAdmin sysAdmin;
        if (StringUtils.isNotBlank(adminId)) {
            sysAdmin = this.sysAdminMapper.selectByPrimaryKey(adminId);
        } else {
            sysAdmin = new SysAdmin();
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put(KEY_DATA, sysAdmin);
        return executeRerutn(jsonObject);
    }

    public void check(SysAdmin sysAdmin) {

        if (StringUtils.isBlank(sysAdmin.getAdminName())) {
            throw new BusinessException(ErrorMsg.CALL_FAIL, "用户名不能为空");
        }

        if (StringUtils.isBlank(sysAdmin.getPhone())) {
            throw new BusinessException(ErrorMsg.CALL_FAIL, "手机不能为空");
        }

        if (StringUtils.isBlank(sysAdmin.getEmail())) {
            throw new BusinessException(ErrorMsg.CALL_FAIL, "邮箱不能为空");
        }

        if (StringUtils.isBlank(sysAdmin.getPassword())) {
            throw new BusinessException(ErrorMsg.CALL_FAIL, "密码不能为空");
        }

        SysAdminExample sysAdminExample = new SysAdminExample();
        sysAdminExample.or().andAdminNameEqualTo(sysAdmin.getAdminName()).andAdminIdNotEqualTo(sysAdmin.getAdminId());
        sysAdminExample.or().andPhoneEqualTo(sysAdmin.getPhone()).andAdminIdNotEqualTo(sysAdmin.getAdminId());
        sysAdminExample.or().andEmailEqualTo(sysAdmin.getEmail()).andAdminIdNotEqualTo(sysAdmin.getAdminId());

        Long rel = this.sysAdminMapper.countByExample(sysAdminExample);
        if (rel != 0) {
            throw new BusinessException(ErrorMsg.CALL_FAIL, "用户名、手机或邮箱已经被占用");
        }
    }

}
